package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.JdbcCon;
import com.example.demo.common.Result;
import com.example.demo.common.constant.RuleCode;
import com.example.demo.verify.ParamValid;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/jdbc")
@Api(tags = "21、JDBC原生调用")
@ApiSupport(author = "罗海林", order = 21)
@Validated
@RequiredArgsConstructor
public class JDBCController {
    private final JdbcCon rst;

    @PostMapping("/getInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID"),
            @ApiImplicitParam(name = "name", value = "姓名")
    })
    @ApiOperation(value = "获取数据记录信息", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result getInfo(@ParamValid(name = "用户ID", rule = RuleCode.noEmpty) @RequestParam String userId,
                          @ParamValid(name = "姓名", rule = RuleCode.noEmpty) @RequestParam String name) throws Exception {
        rst.addParam(userId);
        rst.addParam(name);
        JSONObject js = rst.getInfo("*", "user", "user_id = ? and name = ?");

        rst.addParam(1);
        JSONArray list1 = rst.getList("user_id, username, name, role_id, token_id", "user", "is_use = ?");

        rst.addParam(0);
        JSONArray list2 = rst.getList("user_id, username, name, role_id, token_id", "user", "is_use = ?");

        Result res = new Result().ok();
        res.put("info", js);
        res.put("list1", list1);
        res.put("list2", list2);
        return res;
    }

    @PostMapping("/execute")
    @ApiOperation(value = "执行SQL语句", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    public Result execute() throws Exception {
        rst.setSql("insert into user (name, username, password) values(?, ?, ?)");
        rst.addParam("全晴");
        rst.addParam("quangqing");
        rst.addParam("214510115lhl");
        int num = rst.execute();

        Result res = new Result().ok();
        res.put("num", num);
        return res;
    }

    @PostMapping("/insertBatch")
    @ApiOperation(value = "批量插入数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 3)
    public Result insertBatch() throws Exception {
        rst.setSql("insert into user (username, name) values (?, ?)");

        List<List<Object>> paramList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            List<Object> param = new ArrayList<>();
            param.add("全晴" + i);
            param.add("林春丽" + i);

            paramList.add(param);
        }

        rst.insertBatch(paramList);

        Result res = new Result().ok();
        return res;
    }

    @PostMapping("/deleteInfo")
    @ApiOperation(value = "删除记录", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 4)
    public Result deleteInfo() throws Exception {
        rst.addCollect("username", "quangqing1");
        rst.addCollect("name", "全晴");
        int num = rst.deleteInfo("user");

        Result res = new Result().ok();
        res.put("num", num);
        return res;
    }

    @PostMapping("/updateInfo")
    @ApiOperation(value = "修改记录", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 5)
    public Result updateInfo() throws Exception {
        rst.addCollect("name", "小楚乔");
        rst.addCollect("password", "214510115lhl");
        int num = rst.updateInfo("user");

        Result res = new Result().ok();
        res.put("num", num);
        return res;
    }

    @PostMapping("/insertInfo")
    @ApiOperation(value = "新增记录", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 6)
    public Result insertInfo() throws Exception {
        rst.addCollect("user_id", "0501100229");
        rst.addCollect("name", "陈艺文");
        int num = rst.insertInfo("user");

        Result res = new Result().ok();
        res.put("num", num);
        return res;
    }
}
